package com.example.bmi;

import static java.lang.System.out;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.math.BigDecimal;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "MainActivity";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView show= findViewById(R.id.show);
        EditText input = findViewById(R.id.height);
        EditText input1 = findViewById(R.id.weight);
        TextView txt = findViewById(R.id.txt);

        Button btn= findViewById(R.id.btn);
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        Log.i("myClick", "onClick:AAAAAAA");
        String low = "BMI指数过低，\n建议补充蛋白质！" ;
        String normal = "BMI指数正常，\n建议保持饮食习惯以及适量的运动！" ;
        String larger = "BMI指数过高，\n建议注意您的饮食，增加运动量！" ;
        String fat = "BMI指数超高，\n建议您多运动消耗热量！" ;


        EditText editHeight = (EditText)findViewById(R.id.height);
        EditText editWeight = (EditText)findViewById(R.id.weight);
        TextView txt = (TextView)findViewById(R.id.txt);

        Double height = Double.parseDouble(editHeight.getText().toString());
        Double weight = Double.parseDouble(editWeight.getText().toString());
        Double bmi = weight / (height*height);

        BigDecimal b= new BigDecimal(bmi);
        float bmi1 = b.setScale(2,BigDecimal.ROUND_HALF_UP).floatValue();
        if(bmi<18.5){
            txt.setText( "您的BMI指数为："+String.format("%.2f",bmi1)+"\n"+low);
        }else  if (bmi1>=18.5&&bmi1<=22.9){
            txt.setText("您的BMI指数为："+String.format("%.2f",bmi1)+"\n" +normal);
        }else  if (bmi1>=23&&bmi1<=24.9){
            txt.setText("您的BMI指数为："+String.format("%.2f",bmi1)+"\n"+larger);
        }else{
            txt.setText("您的BMI指数为："+String.format("%.2f",bmi1)+"\n"+fat);
        }











        }
    }

